<template>
  <div class="container">
    <div class="e-background" id="erpBackground">
      <div
        v-for="(flow, index) in dataFlows"
        :key="'flow-' + index"
        class="data-flow"
        :style="{ left: flow.left, animationDelay: flow.delay }"
      ></div>
      <div
        v-for="(logo, index) in logoElements"
        :key="'logo-' + index"
        class="company-logo"
        :style="{
          left: logo.left,
          top: logo.top,
          animationDelay: logo.animationDelay,
          transform: logo.transform,
          backgroundImage: 'url(' + logoImage + ')',
        }"
      ></div>
    </div>

    <div class="login-container">
      <!-- 左侧信息面板 -->
      <div class="info-panel">
        <!-- <div class="company-name">亚新钢铁</div> -->
        <h1 class="system-name">亚新集团</h1>
        <h1 class="system-name">外协管理系统</h1>
        <div class="system-subtitle">协同无界 · 管理有方 · 运营未来</div>

        <div class="features">
          <div class="feature-item">
            <div class="feature-icon">✓</div>
            <span>诚信为本-诚信是立业之本</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon">✓</div>
            <span>质量为基-质量是兴业之基</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon">✓</div>
            <span>高效协同-内部协同,精诚合作,高效执行</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon">✓</div>
            <span>以人为本-致力于为客户创造价值,为员工赋能成长</span>
          </div>
        </div>
      </div>

      <!-- 右侧表单面板 -->
      <div class="panel">
        <div class="header">
          <div class="title">用户登录</div>
        </div>

        <form class="login-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <input
              type="text"
              id="username"
              v-model="loginForm.username"
              placeholder=" "
              autocomplete="off"
            />
            <label class="form-label" for="username">用户账号</label>
          </div>

          <div class="form-group">
            <input
              type="password"
              id="password"
              v-model="loginForm.password"
              placeholder=" "
              autocomplete="off"
            />
            <label class="form-label" for="password">登录密码</label>
          </div>

          <div class="captcha-group">
            <div class="captcha-input">
              <input
                type="text"
                id="captcha"
                v-model="loginForm.code"
                placeholder=" "
                autocomplete="off"
              />
              <label class="captcha-label" for="captcha">验证码</label>
            </div>
            <div
              class="captcha-math"
              id="captchaMath"
              title="点击刷新验证码"
              @click="getCode"
            >
              <img :src="codeUrl" class="login-code-img" />
            </div>
          </div>

          <button type="submit" class="login-btn" :disabled="loggingIn">
            {{ loggingIn ? "登录中..." : "登 录 系 统" }}
          </button>
        </form>

        <div class="footer">
          <div class="support-info">
            技术支持：信息化部 | 服务热线：0518-80927112
          </div>
          <div class="copyright">© 2025 连云港亚新钢铁有限公司 版权所有</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
// Cookie 操作封装
const cookieOptions = { expires: 30, secure: true, sameSite: "Strict" };

export default {
  name: "LoginPage",
  data() {
    return {
      username: "",
      password: "",
      captcha: "",
      loggingIn: false,
      // 背景logo地址
      logoImage: require("@/assets/logo/login-logo.png"),
      dataFlows: [],
      logoElements: [],

      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      // 验证码开关
      captchaEnabled: true,
    };
  },
  created() {
    this.getCode();
    // this.getCookie();
  },
  mounted() {
    this.createERPBackground();

    // 输入框动画效果
    const inputs = document.querySelectorAll("input");
    inputs.forEach((input) => {
      input.addEventListener("focus", () => {
        input.parentElement.style.transform = "translateY(-2px)";
      });
      input.addEventListener("blur", () => {
        input.parentElement.style.transform = "translateY(0)";
      });
    });
  },
  methods: {
    createERPBackground() {
      // 创建数据流动画
      for (let i = 0; i < 8; i++) {
        this.dataFlows.push({
          left: `${Math.random() * 100}%`,
          delay: `${Math.random() * 3}s`,
        });
      }

      // 创建logo
      this.createLogoElements();
    },

    createLogoElements() {
      // 创建多个logo
      for (let i = 0; i < 12; i++) {
        this.logoElements.push({
          left: `${Math.random() * 90 + 5}%`,
          top: `${Math.random() * 70 + 15}%`,
          animationDelay: `${Math.random() * 8}s`,
          transform: `scale(${Math.random() * 0.5 + 0.8})`,
        });
      }
    },

    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.data.captchaEnabled === undefined
            ? true
            : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },

    getCookie() {
      const rememberMe = Cookies.get("rememberMe") == "true";
      if (rememberMe) {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        this.loginForm = {
          username: username || this.loginForm.username,
          password: password ? decrypt(password) : "",
          rememberMe,
        };
      }
    },

    loginCheck() {
      if (this.loginForm.username == "" || this.loginForm.username == null) {
        this.$modal.msgWarning("请输入用户账号！");
        return false;
      }
      if (this.loginForm.password == "" || this.loginForm.password == null) {
        this.$modal.msgWarning("请输入登录密码！");
        return false;
      }
      if (this.loginForm.code == "" || this.loginForm.code == null) {
        this.$modal.msgWarning("请输入验证码！");
        return false;
      }
      return true;
    },

    handleLogin() {
      if (this.loginCheck()) {
        this.loggingIn = true;
        // 加密密码
        const encryptedPwd = encrypt(this.loginForm.password);
        // 不修改 this.loginForm，构造请求体
        const payload = { ...this.loginForm, password: encryptedPwd };

        if (this.loginForm.rememberMe) {
          Cookies.set("username", this.loginForm.username, cookieOptions);
          Cookies.set("password", encryptedPwd, cookieOptions);
          Cookies.set("rememberMe", true, cookieOptions);
        } else {
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove("rememberMe");
        }
        this.$store
          .dispatch("Login", payload)
          .then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => {});
          })
          .catch(() => {
            this.loggingIn = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
      }
    },
  },
};
</script>

<style scoped>
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 50%, #cbd5e1 100%);
  overflow: hidden;
  color: #2d3748;
  position: relative;
}
.e-background {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 0;
  overflow: hidden;
  background: linear-gradient(
      90deg,
      rgba(66, 153, 225, 0.1) 1px,
      transparent 1px
    ),
    linear-gradient(rgba(66, 153, 225, 0.1) 1px, transparent 1px);
  background-size: 40px 40px;
}
/* 数据流动画 */
.data-flow {
  position: absolute;
  width: 2px;
  height: 100px;
  background: linear-gradient(to bottom, transparent, #4299e1, transparent);
  animation: dataFlow 3s linear infinite;
  opacity: 0.6;
}
@keyframes dataFlow {
  0% {
    transform: translateY(-100px);
  }
  100% {
    transform: translateY(100vh);
  }
}
/* 登录容器 */
.login-container {
  position: relative;
  width: 900px;
  height: 520px;
  display: flex;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(66, 153, 225, 0.2),
    0 0 30px rgba(255, 255, 255, 0.8), inset 0 0 0 1px rgba(255, 255, 255, 0.6);
  z-index: 10;
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  overflow: hidden;
}
/* 左侧信息区域 */
.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #2c5282, #4299e1);
  color: white;
  padding: 45px 35px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  overflow: hidden;
}
.info-panel::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100" opacity="0.05"><circle cx="50" cy="50" r="40" stroke="white" stroke-width="2" fill="none" /><rect x="30" y="30" width="40" height="40" stroke="white" stroke-width="1" fill="none" /></svg>');
  background-size: 200px;
  opacity: 0.1;
}
.company-name {
  font-size: 26px;
  font-weight: bold;
  margin-bottom: 15px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}
.system-name {
  font-size: 38px;
  font-weight: bold;
  margin-bottom: 20px;
  margin-top: 0px;
  letter-spacing: 1px;
}
.system-subtitle {
  font-size: 16px;
  margin-bottom: 30px;
  font-weight: 500;
  letter-spacing: 0.5px;
  line-height: 1.6;
}
.features {
  margin-top: 30px;
}
.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  font-size: 15px;
}
.feature-icon {
  margin-right: 12px;
  width: 24px;
  height: 24px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* 右侧表单区域 */
.panel {
  flex: 1;
  padding: 45px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.header {
  text-align: center;
  margin-bottom: -9px;
}
.title {
  font-size: 26px;
  font-weight: bold;
  color: #2c5282;
  margin-bottom: 10px;
}
.subtitle {
  font-size: 14px;
  color: #718096;
}
/* 表单样式 */
.login-form {
  margin-top: 10px;
}
.form-group {
  margin-bottom: 28px;
  position: relative;
}
.form-group input {
  width: 100%;
  padding: 18px 25px;
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid #e2e8f0;
  border-radius: 10px;
  color: #2d3748;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(66, 153, 225, 0.1);
}
.form-group input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 20px rgba(66, 153, 225, 0.2);
  transform: translateY(-2px);
}
.form-group input::placeholder {
  color: #a0aec0;
}
.form-label {
  position: absolute;
  left: 25px;
  top: 18px;
  color: #a0aec0;
  pointer-events: none;
  transition: all 0.3s ease;
  background: white;
  padding: 0 8px;
  font-weight: 500;
}
.form-group input:focus + .form-label,
.form-group input:not(:placeholder-shown) + .form-label {
  top: -12px;
  left: 20px;
  font-size: 14px;
  color: #4299e1;
  font-weight: bold;
}
/* 验证码区域 */
.captcha-group {
  display: flex;
  gap: 15px;
  margin-bottom: 30px;
  position: relative;
}
.captcha-input {
  flex: 1;
  position: relative;
}
.captcha-input input {
  width: 100%;
  padding: 18px 25px;
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid #e2e8f0;
  border-radius: 10px;
  color: #2d3748;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(66, 153, 225, 0.1);
  height: 40px;
}
.captcha-label {
  position: absolute;
  left: 25px;
  top: 8px;
  color: #a0aec0;
  pointer-events: none;
  transition: all 0.3s ease;
  background: white;
  padding: 0 8px;
  font-weight: 500;
}
.captcha-input input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 20px rgba(66, 153, 225, 0.2);
  transform: translateY(-2px);
}
.captcha-input input::placeholder {
  color: #a0aec0;
}
.captcha-math {
  width: 160px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 2px solid #4299e1;
  user-select: none;
  font-family: "Courier New", monospace;
  font-size: 24px;
  font-weight: bold;
  color: #2c5282;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(66, 153, 225, 0.15);
}
.captcha-math:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(66, 153, 225, 0.25);
}
.captcha-group input:focus + .captcha-label,
.captcha-group input:not(:placeholder-shown) + .captcha-label {
  top: -12px;
  left: 20px;
  font-size: 14px;
  color: #4299e1;
  font-weight: bold;
}
.login-code-img {
  height: 36px;
}
/* 登录按钮 */
.login-btn {
  width: 100%;
  padding: 20px;
  background: linear-gradient(135deg, #2c5282, #4299e1);
  border: none;
  border-radius: 12px;
  color: white;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 2px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
  box-shadow: 0 8px 25px rgba(66, 153, 225, 0.3);
  position: relative;
  overflow: hidden;
}
.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 30px rgba(66, 153, 225, 0.4);
  background: linear-gradient(135deg, #4299e1, #2c5282);
}
.login-btn:active {
  transform: translateY(-1px);
}
.login-btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.4),
    transparent
  );
  transition: 0.5s;
}
.login-btn:hover::before {
  left: 100%;
}
/* 底部信息 */
.footer {
  text-align: center;
  margin-top: 0px;
  padding-top: 25px;
  /* border-top: 2px solid rgba(66, 153, 225, 0.2); */
}
.support-info {
  font-size: 14px;
  color: #4a5568;
  margin-bottom: 12px;
  font-weight: 500;
}
.copyright {
  font-size: 12px;
  color: #718096;
  font-weight: 500;
}
.company-logo {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  opacity: 0;
  animation: floatAndFade 8s ease-in-out infinite;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
@keyframes floatAndFade {
  0% {
    transform: translateY(0) rotate(0deg) scale(0.8);
    opacity: 0;
  }
  20% {
    opacity: 0.7;
  }
  50% {
    transform: translateY(-40px) rotate(5deg) scale(1);
    opacity: 0.9;
  }
  80% {
    opacity: 0.5;
  }
  100% {
    transform: translateY(-80px) rotate(10deg) scale(1.2);
    opacity: 0;
  }
}
/* 响应式设计 */
@media (max-width: 920px) {
  .login-container {
    width: 90%;
    height: auto;
    flex-direction: column;
    margin: 20px;
  }
  .info-panel {
    padding: 30px 25px;
  }
  .panel {
    padding: 30px 25px;
  }
  .system-name {
    font-size: 32px;
  }
  .captcha-group {
    flex-direction: column;
  }
  .captcha-math {
    width: 100%;
    height: 55px;
  }
}
</style>
